package team.gl.nio.svr;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.ReferenceCountUtil;

import java.util.Vector;

import com.richman.controller.DecreseRatio;
import com.richman.hg.HGManager;

import team.gl.nio.cmn.Bag;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        //System.out.println("server channelRead..");
//        Bag us = (Bag) msg;
//        if(us.getReq().equals("register")) {
//        	
//            NettyServer.addClientChannel(ctx, us.getClientname(), us.getCriedits());
//            
//            System.out.println("a new connect come in remote ip: " + ctx.channel().remoteAddress());	    	
//        }else if(us.getReq().equals("betres")){
//        	//
//        	System.out.println(us.toString());
//        }else if(us.getReq().equals("gamedetails")){
//        	
//        	System.out.println("gamedetails request");
//  
//        	Bag bag = new Bag("gamedetails");
//
//        	
//        	ctx.channel().writeAndFlush(bag);
//
//        	//System.out.println(bag);
//        }else if(us.getReq().equals("pdataRatio")){
//        	
//        	System.out.println("pdataRatio request");
//        	
//        	Bag bag = new Bag("pdataRatio");
//        	
//        	ctx.channel().writeAndFlush(bag);
//        }else if(us.getReq().equals("dxqgames")){
//        	Bag bag = new Bag("pdataRatio");
//        	
//        	ctx.channel().writeAndFlush(bag);
//        	
//        }else if(us.getReq().equals("specialPankougames")){
//        	Bag bag = new Bag("specialPankougames");
//        	bag.setSenddatas(DaShui.hgps38EventManager.getsenddatas());
//        	bag.setHGEvents(HGManager.gethgevents());
//        	ctx.channel().writeAndFlush(bag);
//        }
    	Bag bag = new Bag("leaguesEvents");
    	bag.setForbidLeagues(DecreseRatio.mainWnd.getForbidLeagues());
    	bag.setNegLeagues(DecreseRatio.mainWnd.getNegLeagues());
    	ctx.channel().writeAndFlush(bag);    
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        //System.out.println("server channelReadComplete..");
        ctx.flush();//ˢ�º�Ž���ݷ�����SocketChannel
    }
    

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        //System.out.println("server exceptionCaught.." + cause.getMessage());
        ctx.close();
    }
    
    
    
    
    // 心跳丢失计数器
    private int counter;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //System.out.println("--- Client is active ---");
    	

    	
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
       
    	String uuid = ctx.channel().id().asLongText();
    	NettyServer.removeClientChannel(uuid);
/*        System.out.println("a connect disconnet: " + uuid);
        System.out.println("a connect disconnet ip: " + ctx.channel().remoteAddress());*/
    	
    }



    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            // 空闲6s之后触发 (心跳包丢失)
            if (counter >= 3) {
                // 连续丢失3个心跳包 (断开连接)
                ctx.channel().close().sync();
                System.out.println("已与Client断开连接");
            } else {
                counter++;
                System.out.println("丢失了第 " + counter + " 个心跳包");
            }
        }
    }





}
